<?php

    require_once(dirname(__FILE__) . '/../includes/common.inc.php');

    if ( Request::IsMethod(Request::POST) )
    {
        ProcessForm();
    }

    $categoryId = Request::GetParam('id');
    $category = $categoryId ? ArticleCategoryTable::GetCategoryById( $categoryId ) : new ArticleCategory();
    $title    = $category ? 'Edit Category' : 'New Category';
    $name     = $category->GetName();
    $language = $category->GetLanguage();

    $page = Pages::CreatePage( $pageInfo->id );

    $page->SetVar('category_id', $categoryId);
    $page->SetVar('title', $title);
    $page->SetVar('name', $name);
    $page->SetVar('language', $language);
    $page->SetArrVar('languages', CodeDriveSiteLanguage::GetSupportedLanguages());

    $page->Display();

    function ProcessForm()
    {
        $categoryId = Request::GetParam('id');

        if ( Request::GetParam('action') == 'Delete' )
        {
            if ( $categoryId )
            {
                ArticleCategoryTable::Delete( $categoryId );

                Request::Redirect( Pages::GetPageUrl('AdminCategories') );
            }
        }

        $category = new ArticleCategory();
        $category->SetCategoryId( $categoryId );
        $category->SetName( trim(Request::GetParam('name')) );
        $category->SetLanguage( trim(Request::GetParam('language')) );
        
        $categoryId = ArticleCategoryTable::SaveCategory( $category );

        Request::Redirect( Pages::GetPageUrl( 'AdminCategory', array('id' => $categoryId) ) );
    }
?>